package com.shuai.jit.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shuai.jit.entity.Classes;
import com.shuai.jit.entity.College;
import com.shuai.jit.entity.R;
import com.shuai.jit.mapper.ClassesMapper;
import com.shuai.jit.mapper.CollegeMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@Api("学院班级控制类")
@RequestMapping("/user")
public class CollegeController {

    @Autowired
    CollegeMapper collegeMapper;

    @Autowired
    ClassesMapper classesMapper;

    @ApiOperation("获取学校的所有学院信息")
    @RequestMapping("/getCollege")
    public R getCollege(){
        return R.ok().data("college",collegeMapper.selectList(null));
    }

    @ApiOperation("获取学校的所有学院信息")
    @RequestMapping("/addCollege")
    public R addCollege(@RequestBody College college){

        if(college==null||college.getCollegename()==null){
            return R.error().data("college","对不起数据为空");
        }

        Integer result=collegeMapper.insert(college);
        if(result>0){
            return  R.ok().data("college","学院插入成功");
        }else {
            return R.ok().data("college","学院插入失败");
        }
    }

    @ApiOperation("获取学校的所有学院信息")
    @RequestMapping("/addClasses")
    public R addClasses(@RequestBody Classes classes){

        log.info(classes.toString());
        if(classes==null||classes.getPid()==null||classes.getClassesname()==null){
            return R.error().data("classes","对不起数据为空");
        }

        College college=collegeMapper.selectById(classes.getPid());

        if(college==null){
            return R.error().data("classes","对不起查不到该学院");
        }

        Integer result=classesMapper.insert(classes);
        if(result>0){
            return  R.ok().data("classes","班级插入成功");
        }else {
            return R.ok().data("classes","班级插入失败");
        }
    }


    @ApiOperation("获取学院的班级信息信息")
    @RequestMapping("/getClasses")
    public R getClasses(Long id){
        QueryWrapper<Classes> classesQueryWrapper=new QueryWrapper<>();
        classesQueryWrapper.eq("pid",id);
        List<Classes> classesList=classesMapper.selectList(classesQueryWrapper);
        if(classesList!=null&&classesList.size()>0){
            return R.ok().data("classes",classesList);
        }else {
            return R.ok().data("classes","对不起查不到该院校下的班级信息");
        }

    }
}
